<?php
namespace extend;

class ImageCut{

    private $params = array('save_type'=>'jpg');

    function __construct(array $config = array()){
        $this->params = array_merge($this->params, $config);
    }

    /**
     * 裁剪图片
     * @param $source_file  //欲裁剪的文件
     * @param $x  //欲裁剪的x轴位置
     * @param $y  //欲裁剪的y轴位置
     * @param $w  //欲裁剪的宽
     * @param $h  //欲裁剪的高
     * @param $offset_w //页面拉伸的高度
     * @param $offset_h  //页面拉伸的宽度
     * @return string   //结果文件名
     * @throws \Exception
     */
    function cut($source_file, $x, $y, $w, $h, $offset_w = false, $offset_h = false){
        $source_info = getimagesize($source_file);
        $source = $this->source2image($source_file);
        $image = imagecreatetruecolor($w, $h);
        if($offset_w && $offset_h){     //如果图片和页面显示比例不一样
            $cache = imagecreatetruecolor($offset_w, $offset_h);
            imagecopyresampled($cache, $source, 0, 0, 0, 0, $offset_w, $offset_h, $source_info[0], $source_info[1]);
            $result = imagecopy($image, $cache, 0, 0, $x, $y, $w, $h);
        }else{
            $result = imagecopy($image, $source, 0, 0, $x, $y, $w, $h);
        }
        if(!$result){ throw new \Exception('裁剪图片失败!'); }
        return $this->saveImage($image);
    }

    /**
     * 生成缩略图
     * @param array $params //缩略图的宽和高['90(缩略图1的宽), 90(缩略图1的高)', '60(缩略图2的宽), 40']
     * @param $image
     * @return array
     * @throws \Exception
     */
    function thumb(array $params, $image){
        $result = array();
        $source_info = getimagesize($image);
        $source = $this->source2image($image);
        foreach($params as $val){
            $cache = explode(',', $val);
            $thumb = imagecreatetruecolor($cache[0], $cache[1]);
            imagecopyresampled($thumb, $source, 0, 0, 0, 0, $cache[0], $cache[1], $source_info[0], $source_info[1]);
            if($filename = $this->saveImage($thumb, "@{$cache[0]}_{$cache[1]}")){
                array_push($result, $filename);
            }else{
                throw new \Exception('保存缩略图失败!');
            }
        }
        return $result;
    }

    /**
     * 从源文件创建图片资源
     * @param $source_file
     * @return resource
     * @throws \Exception
     */
    function source2image($source_file){
        switch($this->getSuffix($source_file)){
            case 'jpeg': case 'jpg': $source = imagecreatefromjpeg($source_file); break;
            case 'png': $source = imagecreatefrompng($source_file); break;
            case 'gif': $source = imagecreatefromgif($source_file); break;
            default: throw new \Exception('不支持的类型!');
        }
        return $source;
    }

    /**
     * 生成文件保存名
     * @param bool $suffix
     * @param $ext
     * @return string
     * @throws \Exception
     */
    function saveName($suffix = false, $ext = ''){
        $suffix = $suffix ? $suffix : 'jpg';
        if(!is_dir($this->params['path'])){ throw new \Exception('配置保存路径错误!'); }
        $path = $this->params['path'] . '/' . date('Ymd') . '/';
        is_dir($path) || mkdir($path);
        return  $path . time() . "{$ext}.{$suffix}";
    }

    /**
     * 保存图像文件
     * @param $recourse
     * @param $ext
     * @return string
     * @throws \Exception
     */
    function saveImage($recourse, $ext = ''){
        switch($this->params['save_type']){
            case 'jpg':
            case 'jpeg':
                $filename = $this->saveName('jpg', $ext);
                if(!imagejpeg($recourse, $filename)){ throw new \Exception('保存文件失败!'); }
                break;
            case 'png':
                $filename = $this->saveName('png', $ext);
                if(!imagepng($recourse, $filename)){ throw new \Exception('保存文件失败!'); }
                break;
            case 'gif':
                $filename = $this->saveName('gif', $ext);
                if(!imagegif($recourse, $filename)){ throw new \Exception('保存文件失败!'); }
                break;
            default: throw new \Exception('不支持的保存类型!');
        }
        return $filename;
    }

    /**
     * 获取文件后缀名
     * @param $file
     * @return mixed
     */
    function getSuffix($file){
        return pathinfo($file)['extension'];
    }

}